package org.sample.springmvc.controller;

import java.util.List;
import java.util.Map.Entry;

import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(path="/mvc")
public class SampleController {

	@RequestMapping(path = "/text", method = RequestMethod.GET)
	@ResponseBody
	public String getText(@RequestParam(name="value") String text) {
		return text;
	}

	@RequestMapping(path = "/view", method = RequestMethod.GET)
	public String getView(@RequestParam(name="value") String viewName) {
		return viewName;
	}

	@RequestMapping(path = "/view/{view}", method = RequestMethod.GET)
	public ModelAndView getView(@PathVariable(name = "view") String viewName,
			@RequestParam MultiValueMap<String, String> params) {
		ModelAndView modelAndView = new ModelAndView(viewName);

		for (Entry<String, List<String>> entry : params.entrySet()) {
			modelAndView.addObject(entry.getKey(), entry.getValue());
		}

		return modelAndView;
	}
	
}
